package com.scau402.system.controller;

import com.scau402.common.annotation.Log;
import com.scau402.common.constant.OutboundConstants;
import com.scau402.common.core.controller.BaseController;
import com.scau402.common.core.domain.AjaxResult;
import com.scau402.common.core.page.TableDataInfo;
import com.scau402.common.enums.BusinessType;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.poi.ExcelUtil;
import com.scau402.system.domain.WmsJade;
import com.scau402.system.domain.WmsOutboundOrder;
import com.scau402.system1.domain.po.WmsWarehouseStoragePO;
import com.scau402.system1.domain.po.WmsWarehouseDO;
import com.scau402.system.service.ISysTenantService;
import com.scau402.system.service.IWmsOutboundOrderService;
import com.scau402.system1.service.impl.WmsWarehouseStorageService;
import com.scau402.system1.service.impl.WmsWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品出库单Controller
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/system/outboundOrder")
public class WmsOutboundOrderController extends BaseController {
    @Autowired
    private IWmsOutboundOrderService wmsOutboundOrderService;

    @Autowired
    private ISysTenantService tenantService;

    @Autowired
    private WmsWarehouseService wmsWarehouseService;

    @Autowired
    private WmsWarehouseStorageService wmsWarehouseStorageService;

    /**
     * 查询商品出库单列表
     */
    @PreAuthorize("@ss.hasPermi('system:outboundOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsOutboundOrder wmsOutboundOrder) {
        startPage();
        List<WmsOutboundOrder> list = wmsOutboundOrderService.selectWmsOutboundOrderList(wmsOutboundOrder);
        return getDataTable(list);
    }

    /**
     * 导出商品出库单列表
     */
    @PreAuthorize("@ss.hasPermi('system:outboundOrder:export')")
    @Log(title = "商品出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsOutboundOrder wmsOutboundOrder) {
        List<WmsOutboundOrder> list = wmsOutboundOrderService.selectWmsOutboundOrderList(wmsOutboundOrder);
        ExcelUtil<WmsOutboundOrder> util = new ExcelUtil<WmsOutboundOrder>(WmsOutboundOrder.class);
        util.exportExcel(response, list, "商品出库单数据");
    }

    /**
     * 获取商品出库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:outboundOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wmsOutboundOrderService.selectWmsOutboundOrderById(id));
    }

    /**
     * 新增商品出库单
     */
    @PreAuthorize("@ss.hasPermi('system:outboundOrder:add')")
    @Log(title = "商品出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsOutboundOrder wmsOutboundOrder) {
        // 步骤：1
        // 设置租户ID
        wmsOutboundOrder.setTenantId(getLoginUser().getTenantId());

        // 出库数量为0
        Long totalQuantity = 0L;
        for (WmsJade jade : wmsOutboundOrder.getJades()) {
            totalQuantity += jade.getJadeQuantity();
        }

        if (totalQuantity == OutboundConstants.OUTBOUND_QUANTITY_ZERO) {
            return error("出库货品数量为0");
        }

//        // 检查仓库是否允许出库
//        if(!wmsWarehouseService.checkOutOK(wmsOutboundOrder)) {
//            return error("目的仓库类型为不准出库");
//        }

//         检查货品出库数量是否大于库存数量
//        if(wmsWarehouseService.checkOutQuantity(wmsOutboundOrder)) {
//
//        }

        // 已还货数量


        // 市场还货数量


        // 市场未还货数量


        // 更新仓库数量
        // 借货出库
        // 设置市场借货数量、市场未还数量、市场已还数量
//        if(wmsOutboundOrder.getOrderType().equals(OutboundConstants.OUTBOUND_ORDER_TYPE_LEND)) {
//            wmsOutboundOrder
//        }
//        // 还货出库
//        if(wmsOutboundOrder.getOrderType().equals(OutboundConstants.OUTBOUND_ORDER_TYPE_RETURN)) {
//
//        }
//        // 销售出库
//        if(wmsOutboundOrder.getOrderType().equals(OutboundConstants.OUTBOUND_ORDER_TYPE_SELL)) {
//
//        }
        wmsOutboundOrder.setJadeQuantity(totalQuantity);

        for (WmsJade jade : wmsOutboundOrder.getJades()) {
            // 更新仓位数量
            WmsWarehouseStoragePO storage = wmsWarehouseStorageService.selectWmsWarehouseStorageById(jade.getStorageId());
            storage.setUsedCap(storage.getUsedCap() - jade.getJadeQuantity());
            wmsWarehouseStorageService.updateWmsWarehouseStorage(storage);
            // 更新仓库数量
            WmsWarehouseDO warehouse = wmsWarehouseService.selectWmsWarehouseById(jade.getWarehouseId());
            warehouse.setUsedCap(warehouse.getUsedCap() - jade.getJadeQuantity());
            wmsWarehouseService.updateWmsWarehouse(warehouse);
        }

        return toAjax(wmsOutboundOrderService.insertWmsOutboundOrder(wmsOutboundOrder));
    }

    /**
     * 修改商品出库单
     */
    @PreAuthorize("@ss.hasPermi('system:outboundOrder:edit')")
    @Log(title = "商品出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsOutboundOrder wmsOutboundOrder) {
        return toAjax(wmsOutboundOrderService.updateWmsOutboundOrder(wmsOutboundOrder));
    }

    /**
     * 删除商品出库单
     */
    @PreAuthorize("@ss.hasPermi('system:outboundOrder:remove')")
    @Log(title = "商品出库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wmsOutboundOrderService.deleteWmsOutboundOrderByIds(ids));
    }

    /**
     * 获取出库单编码
     *
     * @return
     * @throws InterruptedException
     */
    @PreAuthorize("@ss.hasPermi('system:outboundOrder:add')")
    @GetMapping("/getOutboundOrderCode")
    public String getOutboundOrderCode() throws InterruptedException {
        // 返回租户编码
        return wmsOutboundOrderService.getNewCode(SecurityUtils.getLoginUser().getUser());
    }
}
